package activemq.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;

@Service
public class ActiveProducer {
    @Autowired
    private JmsTemplate jmsTemplate;
    @Resource(name = "destinationQueue")
    private Destination destinationQueue;

    public void sendMessage(Destination destination, final String msg) {
        try {
            if(destination==null){
                destination = destinationQueue;
            }
            System.out.println("*********send message to " + destination.getClass().getName());
            jmsTemplate.send(destination, session -> session.createTextMessage(msg));
        }catch (Exception e){
            System.out.println("*********message send failed!********");
        }
    }
}
